package org.jeecg.zhenxuan.Service.impl;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.zhenxuan.Entity.PointFlow;
import org.jeecg.zhenxuan.Repository.PointAddRepository;
import org.jeecg.zhenxuan.Service.PointFlowAddService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.*;
import java.util.Date;
import java.util.List;

@Service
public class PointFlowAddServiceImpl implements PointFlowAddService {

    @Autowired
    private PointAddRepository pointRepository;

    /**
     * 根据明细id查询操作明细的信息
     * @param pointFlowId
     * @return
     */
    @Override
    public PointFlow getPointFlowById(String pointFlowId,String operation) {

        PointFlow pointFlow = pointRepository.getPointFlowById(Integer.parseInt(pointFlowId));

        return pointFlow;
    }

    /**
     * 添加操作明细
     * @param openId
     * @param count
     * @param operation
     * @return
     */
    @Override
    public PointFlow savePointFlow(String openId, Integer count, String operation) {

        //添加明细
        PointFlow pointFlow = new PointFlow();
        pointFlow.setOpenId(openId);
        pointFlow.setCount(count);
        pointFlow.setOperationTime(new Date());
        pointFlow.setOperation(operation);

        return pointRepository.save(pointFlow);
    }

    /**
     * 获得最后一条操作记录
     * @param openId
     * @return
     */
    public String getLastPointFlowId(String openId){

        String  pointFlowId = Integer.toString(pointRepository.getMax(openId));

        return pointFlowId;
    }

    @Override
    public JSONObject listPointFlowDetailStd(String openId, String channel , String pageIndex, String elementCount) {
        //查询条件
        Specification<PointFlow> spec = new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) {

                Path<Object> openIdPath = root.get("openId");
                Path<Object> channelPath = root.get("channel");

                Predicate predicate = criteriaBuilder.and(criteriaBuilder.equal(openIdPath, openId), criteriaBuilder.equal(channelPath, channel));

                return predicate;
            }
        };

        //分页
        Pageable pageable = PageRequest.of(Integer.parseInt(pageIndex),Integer.parseInt(elementCount));

        //分页查询
        Page<PointFlow> page = pointRepository.findAll(spec, pageable);

        //获得分页数据
        List<PointFlow> pointFlowList = page.getContent();
        //不返回openId,设空
        for (PointFlow p:pointFlowList) {
            p.setOpenId(null);
        }
        //获得总条数
        long elementsCount = page.getTotalElements();
        //得到总页数
        int pageTotal = page.getTotalPages();
        //当前页码
        int pageCurrentIndex = Integer.parseInt(pageIndex) + 1;

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("elementsCount",elementsCount);
        jsonObject.put("pageCurrentIndex",pageCurrentIndex);
        jsonObject.put("pageTotal",pageTotal);
        jsonObject.put("pointFlowList",pointFlowList);

        return  jsonObject;
    }

    /**
     * 分页查询操作明细
     * @param openId openid
     * @param pageIndex 当前页码
     * @param elementCount 每页显示的数量
     * @return
     */
    @Override
    public JSONObject listPointFlowDetail(String openId, String pageIndex, String elementCount) {

        //查询条件
        Specification<PointFlow> spec = new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) {

                Path<Object> openIdPath = root.get("openId");

                Predicate predicate = criteriaBuilder.equal(openIdPath, openId);

                return predicate;
            }
        };

        //分页
        Pageable pageable = PageRequest.of(Integer.parseInt(pageIndex),Integer.parseInt(elementCount));

        //分页查询
        Page<PointFlow> page = pointRepository.findAll(spec, pageable);

        //获得分页数据
        List<PointFlow> pointFlowList = page.getContent();
        //不返回openId,设空
        for (PointFlow p:pointFlowList) {
            p.setOpenId(null);
        }
        //获得总条数
        long elementsCount = page.getTotalElements();
        //得到总页数
        int pageTotal = page.getTotalPages();
        //当前页码
        int pageCurrentIndex = Integer.parseInt(pageIndex) + 1;

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("elementsCount",elementsCount);
        jsonObject.put("pageCurrentIndex",pageCurrentIndex);
        jsonObject.put("pageTotal",pageTotal);
        jsonObject.put("pointFlowList",pointFlowList);

        return  jsonObject;
    }

    /**
     * 删除明细
     * @param id
     * @return
     */
    @Override
    public void removeById(String id) {

        pointRepository.removeById(Integer.parseInt(id));

//        return null;
    }


}
